package com.itheima.web.controller;


import com.itheima.common.utils.UtilFuns;
import com.itheima.doamin.system.User;
import com.itheima.service.system.ModuleService;
import com.itheima.service.system.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController extends BaseController{
    @Autowired
    UserService userService;
    @Autowired
    ModuleService moduleService;
    HttpSession session;

//	@RequestMapping("login")
//	public String login(String email,String password) {
//	    if (UtilFuns.isEmpty(email)){
//            return redirectToLoginStr;
//        }
//
//        //访问数据库
//        User user=userService.toLogin(email,password);
//
//	    if (user==null){
//	        request.setAttribute("error","您的账号或者密码错误");
//            return forwardToLoginStr;
//        }
//        //获取菜单栏数据
//        List<Module> modules= moduleService.getModuleByUserId(user);
//        request.getSession().setAttribute("user",user);
//        request.getSession().setAttribute("modules",modules);
//        return "home/main";// /WEB-INF/pages/home/main.jsp
//	}

    @RequestMapping("login")
//    @RequestParam("email") String email, @RequestParam("password") String password
    public @ResponseBody String login(@RequestBody Map<String, String> map) {
        String password=null;
        String email=null;
        if(map.containsKey("email")){
            email = map.get("email").toString();
        }
        if(map.containsKey("password")) {
            password = map.get("password").toString();
        }
        session=request.getSession();
       //是否登录
        if (session.getAttribute("user")!=null && UtilFuns.isEmpty(email)){
            //以登录用户
            return "home/main";
        }

        //输入空的用户
        if (UtilFuns.isEmpty(email) || UtilFuns.isEmpty(password)){
            //跳转登录页面
            return "redirect:/login.jsp";
        }

        try {
            //进行认证
            //获取到subject与shiro安全管理器进行交互
            Subject subject=SecurityUtils.getSubject();
            //获取令牌
            UsernamePasswordToken token=new UsernamePasswordToken(email,password);
            //到数据库进行校验
            subject.login(token);
            //获取到登录的主体
            User user=(User)subject.getPrincipal();
            //将用户存入域中
            session.setAttribute("user",user);
            //获取模块存入域中
            session.setAttribute("modules",moduleService.getModuleByUserId(user));
            //成功
            return "true";
        } catch (Exception e) {
            //登录失败
            request.setAttribute("error","对不起用户名或密码错误");
            e.printStackTrace();
            return "false";
        }
    }

    //去到主页
    @RequestMapping(value = "/toMain",name="去到主页")
    public String toMain(){
        return "home/main";
    }

    //退出
    @RequestMapping(value = "/logout",name="用户登出")
    public String logout(){
        SecurityUtils.getSubject().logout();   //登出
        return "forward:login.jsp";
    }

    @RequestMapping("home")
    public String home(){
	    return "home/home";
    }
}
